package com.art.museumbackend.controller;

import com.art.museumbackend.entity.Articles;
import com.art.museumbackend.entity.PageBean;
import com.art.museumbackend.entity.Result;
import com.art.museumbackend.service.ArticlesService;
import com.art.museumbackend.utils.ResultUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yezi
 * @since 2024-12-07
 */
@RestController
@RequestMapping("/articles")
public class ArticlesController {
    @Autowired
    private ArticlesService articlesService;


    @PostMapping("/add")
    public Result addArticle(@RequestBody @Validated(Articles.Add.class) Articles article) {
        articlesService.addArticle(article);
        return ResultUtil.successMsg("文章添加成功");
    }

    @GetMapping("/list")
    public Result<PageBean<Articles>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer parentColumnId,
            @RequestParam(required = false) Integer subColumnId,
            @RequestParam(required = false) String state
    ) {
        PageBean<Articles> pb =  articlesService.articleList(pageNum,pageSize,parentColumnId,subColumnId,state);
        return ResultUtil.success(pb);
    }


    @GetMapping("/getArticleById")
    public Result<Articles> getArticleDetail(@RequestParam("articleId") Integer articleId) {
        Articles article = articlesService.getArticleById(articleId);
        return ResultUtil.success(article);
    }
    @PutMapping("/updateArticle")
    public Result updateArticle(@RequestBody @Validated(Articles.Update.class) Articles article) {
        articlesService.updateArticle(article);
        return ResultUtil.successMsg("文章修改成功");
    }

    @DeleteMapping("/deleteArticle")
    public Result deleteArticle(@RequestParam("articleId") Integer articleId) {
        articlesService.deleteArticle(articleId);
        return ResultUtil.successMsg("文章删除成功");
    }


}
